<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>NeuZephyr: nz::nodes::calc::GlobalAvgPoolNode Class Reference</title>
<link rel="icon" href="NZ_logo2.png" type="image/x-icon" />
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="NZ_logo2.png"/></td>
  <td id="projectalign">
   <div id="projectname">NeuZephyr
   </div>
   <div id="projectbrief">Simple DL Framework</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="inherits.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><b>nz</b></li><li class="navelem"><a class="el" href="namespacenz_1_1nodes.html">nodes</a></li><li class="navelem"><a class="el" href="namespacenz_1_1nodes_1_1calc.html">calc</a></li><li class="navelem"><a class="el" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">GlobalAvgPoolNode</a></li>  </ul>
</div>
</div><!-- top -->
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node-members.html">List of all members</a>  </div>
  <div class="headertitle"><div class="title">nz::nodes::calc::GlobalAvgPoolNode Class Reference</div></div>
</div><!--header-->
<div class="contents">

<p>Performs global average pooling operation across spatial dimensions of input tensor.  
 <a href="#details">More...</a></p>
<div class="dynheader">
Inheritance diagram for nz::nodes::calc::GlobalAvgPoolNode:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node__inherit__graph.png" border="0" usemap="#anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_inherit__map" alt="Inheritance graph"/></div>
<map name="anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_inherit__map" id="anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_inherit__map">
<area shape="rect" title="Performs global average pooling operation across spatial dimensions of input tensor." alt="" coords="5,80,184,123"/>
<area shape="rect" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="36,5,153,32"/>
<area shape="poly" title=" " alt="" coords="97,48,97,80,92,80,92,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<div class="dynheader">
Collaboration diagram for nz::nodes::calc::GlobalAvgPoolNode:</div>
<div class="dyncontent">
<div class="center"><img src="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node__coll__graph.png" border="0" usemap="#anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_coll__map" alt="Collaboration graph"/></div>
<map name="anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_coll__map" id="anz_1_1nodes_1_1calc_1_1_global_avg_pool_node_coll__map">
<area shape="rect" title="Performs global average pooling operation across spatial dimensions of input tensor." alt="" coords="5,80,184,123"/>
<area shape="rect" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph." alt="" coords="36,5,153,32"/>
<area shape="poly" title=" " alt="" coords="97,48,97,80,92,80,92,48"/>
</map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-methods" name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr class="memitem:ab1e87b4e3649f8a67bfc5a83bb3600e0" id="r_ab1e87b4e3649f8a67bfc5a83bb3600e0"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ab1e87b4e3649f8a67bfc5a83bb3600e0">GlobalAvgPoolNode</a> (<a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *input)</td></tr>
<tr class="memdesc:ab1e87b4e3649f8a67bfc5a83bb3600e0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Constructs a <a class="el" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html" title="Performs global average pooling operation across spatial dimensions of input tensor.">GlobalAvgPoolNode</a> object.  <br /></td></tr>
<tr class="separator:ab1e87b4e3649f8a67bfc5a83bb3600e0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad8b2bb2ce47ab3c227f8c33f3a19fb91" id="r_ad8b2bb2ce47ab3c227f8c33f3a19fb91"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ad8b2bb2ce47ab3c227f8c33f3a19fb91">forward</a> () override</td></tr>
<tr class="memdesc:ad8b2bb2ce47ab3c227f8c33f3a19fb91"><td class="mdescLeft">&#160;</td><td class="mdescRight">Performs the forward pass of the global average pooling operation.  <br /></td></tr>
<tr class="separator:ad8b2bb2ce47ab3c227f8c33f3a19fb91"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7a075619e9c976875118783bbdac8739" id="r_a7a075619e9c976875118783bbdac8739"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a7a075619e9c976875118783bbdac8739">backward</a> () override</td></tr>
<tr class="memdesc:a7a075619e9c976875118783bbdac8739"><td class="mdescLeft">&#160;</td><td class="mdescRight">Performs the backward pass of the global average pooling operation.  <br /></td></tr>
<tr class="separator:a7a075619e9c976875118783bbdac8739"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="inherit_header pub_methods_classnz_1_1nodes_1_1_node"><td colspan="2" onclick="javascript:dynsection.toggleInherit('pub_methods_classnz_1_1nodes_1_1_node')"><img src="closed.png" alt="-"/>&#160;Public Member Functions inherited from <a class="el" href="classnz_1_1nodes_1_1_node.html">nz::nodes::Node</a></td></tr>
<tr class="memitem:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a687ee9c34eb61f8f28caa201ca42696e"><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a687ee9c34eb61f8f28caa201ca42696e">print</a> (std::ostream &amp;os) const</td></tr>
<tr class="memdesc:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Prints the type, data, and gradient of the node.  <br /></td></tr>
<tr class="separator:a687ee9c34eb61f8f28caa201ca42696e inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a9b85913e12422bb4ac2fff483427bb47"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a9b85913e12422bb4ac2fff483427bb47">dataInject</a> (Tensor::value_type *data, bool grad=false) const</td></tr>
<tr class="memdesc:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data into a relevant tensor object, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:a9b85913e12422bb4ac2fff483427bb47 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_a609f1730085dd1d31e0ddcbbae48a065"><td class="memTemplParams" colspan="2">template&lt;typename Iterator &gt; </td></tr>
<tr class="memitem:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#a609f1730085dd1d31e0ddcbbae48a065">dataInject</a> (Iterator begin, Iterator end, const bool grad=false) const</td></tr>
<tr class="memdesc:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data from an iterator range into the output tensor of the InputNode, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:a609f1730085dd1d31e0ddcbbae48a065 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node" id="r_af8b4bab3271df92ca1f0914f7a97b1e8"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classnz_1_1nodes_1_1_node.html#af8b4bab3271df92ca1f0914f7a97b1e8">dataInject</a> (const std::initializer_list&lt; Tensor::value_type &gt; &amp;data, bool grad=false) const</td></tr>
<tr class="memdesc:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="mdescLeft">&#160;</td><td class="mdescRight">Injects data from a std::initializer_list into the output tensor of the <a class="el" href="classnz_1_1nodes_1_1_node.html" title="Base class for nodes in a neural network or computational graph.">Node</a>, optionally setting its gradient requirement.  <br /></td></tr>
<tr class="separator:af8b4bab3271df92ca1f0914f7a97b1e8 inherit pub_methods_classnz_1_1nodes_1_1_node"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Performs global average pooling operation across spatial dimensions of input tensor. </p>
<p>This node reduces each channel's spatial dimensions (H, W) to a single average value, producing output of shape (N, C, 1, 1). Commonly used for final feature aggregation before fully connected layers in CNN architectures.</p>
<p>Core functionality and characteristics:</p><ul>
<li><b>Global Aggregation</b>: Computes channel-wise mean over entire spatial dimensions.</li>
<li><b>Dimensionality Collapse</b>: Reduces H and W dimensions to 1 while preserving channels.</li>
<li><b>Gradient Distribution</b>: Evenly distributes gradients across all spatial positions during backward pass.</li>
<li><b>Parameter-Free</b>: No learnable parameters required for operation.</li>
<li><b>CUDA Support</b>: Optimized GPU implementation for both forward and backward passes.</li>
<li><b>Input Preservation</b>: Maintains batch and channel dimensions unchanged.</li>
</ul>
<p>Key implementation aspects:</p><ul>
<li><b>Forward Pass</b>: Computes spatial average for each channel using tensor reduction.</li>
<li><b>Backward Pass</b>: Scatters gradients by assigning 1/(H*W) of output gradient to each input position.</li>
<li><b>Shape Handling</b>: Automatically handles varying input sizes through dynamic shape inference.</li>
<li><b>Memory Efficiency</b>: Significantly reduces memory footprint for downstream operations.</li>
</ul>
<p>Typical use cases:</p><ul>
<li>Final spatial information aggregation before classification layers.</li>
<li>Network-in-network architectures requiring compact feature representation.</li>
<li>Squeeze-and-Excitation modules for channel-wise attention.</li>
<li>Reducing model parameters in transition layers.</li>
</ul>
<p>Critical considerations:</p><ul>
<li><b>Irreversible Compression</b>: Destroys all spatial information in input features.</li>
<li><b>Input Requirements</b>: Expects 4D input tensor (N, C, H, W).</li>
<li><b>Normalization Factor</b>: Uses 1/(H*W) for both forward average and backward gradient scaling.</li>
</ul>
<dl class="section warning"><dt>Warning</dt><dd><ul>
<li>Input tensor must have spatial dimensions (H, W) &gt; 0.</li>
<li>Not suitable for preserving spatial relationships in features.</li>
<li>May produce non-sensical gradients if applied to single-element spatial dimensions.</li>
</ul>
</dd></dl>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>Output tensor shape: (N, C, 1, 1)</li>
<li>Often used as alternative to flattening+fully connected layers</li>
<li>Particularly effective in combination with convolutional bottlenecks</li>
</ul>
</dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classnz_1_1nodes_1_1calc_1_1_average_pooling_node.html" title="Implements average pooling operation for spatial downsampling in neural networks.">AveragePoolingNode</a> For local spatial averaging with configurable windows </dd>
<dd>
FlattenNode For alternative spatial-to-vector conversion</dd></dl>
<h3><a class="anchor" id="autotoc_md110"></a>
Usage Example:</h3>
<div class="fragment"><div class="line"><span class="comment">// Input features from convolutional backbone</span></div>
<div class="line">InputNode input({16, 512, 7, 7}, <span class="keyword">true</span>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Apply global average pooling</span></div>
<div class="line"><a class="code hl_class" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">GlobalAvgPoolNode</a> gap(&amp;input);</div>
<div class="line">gap.forward();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Output shape becomes (16, 512, 1, 1)</span></div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;Pooled shape: &quot;</span> &lt;&lt; gap.output-&gt;shape() &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Backpropagate through global averaging</span></div>
<div class="line">gap.backward();</div>
<div class="ttc" id="aclassnz_1_1nodes_1_1calc_1_1_global_avg_pool_node_html"><div class="ttname"><a href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">nz::nodes::calc::GlobalAvgPoolNode</a></div><div class="ttdoc">Performs global average pooling operation across spatial dimensions of input tensor.</div><div class="ttdef"><b>Definition</b> <a href="_nodes_8cuh_source.html#l04269">Nodes.cuh:4269</a></div></div>
</div><!-- fragment --><dl class="section author"><dt>Author</dt><dd>Mgepahmge (<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2023/10/20 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cuh_source.html#l04269">4269</a> of file <a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a>.</p>
</div><h2 class="groupheader">Constructor &amp; Destructor Documentation</h2>
<a id="ab1e87b4e3649f8a67bfc5a83bb3600e0" name="ab1e87b4e3649f8a67bfc5a83bb3600e0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab1e87b4e3649f8a67bfc5a83bb3600e0">&#9670;&#160;</a></span>GlobalAvgPoolNode()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">nz::nodes::calc::GlobalAvgPoolNode::GlobalAvgPoolNode </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="classnz_1_1nodes_1_1_node.html">Node</a> *</td>          <td class="paramname"><span class="paramname"><em>input</em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Constructs a <a class="el" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html" title="Performs global average pooling operation across spatial dimensions of input tensor.">GlobalAvgPoolNode</a> object. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">input</td><td>A pointer to the input node. The memory of this pointer is assumed to be managed externally and is used in a read - only manner within this constructor (host - to - host).</td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>None</dd></dl>
<p>This constructor initializes a <a class="el" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html" title="Performs global average pooling operation across spatial dimensions of input tensor.">GlobalAvgPoolNode</a> object. It first adds the provided input node pointer to the <code>inputs</code> vector. Then, it creates a new shared pointer to a Tensor object for the <code>output</code> member. The shape of the output tensor is set to have the same batch size and number of channels as the input tensor's output, but with a height and width of 1. The <code>requiresGrad</code> flag of the output tensor is set to the same value as that of the input tensor's output. Finally, it sets the <code>type</code> member of the node to "GlobalAvgPool".</p>
<p>Memory management strategy: The constructor does not allocate memory for the input node. It only stores a pointer to it. The output tensor is created using <code>std::make_shared</code>, which manages the memory automatically. Exception handling mechanism: There is no explicit exception handling in this constructor. If the <code>std::make_shared</code> call fails to allocate memory for the output tensor, it may throw a <code>std::bad_alloc</code> exception.</p>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">std::bad_alloc</td><td>If memory allocation for the output tensor fails.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>Ensure that the input node pointer is valid and points to a properly initialized node.</li>
<li>The performance of this constructor is mainly determined by the memory allocation for the output tensor, which has a time complexity of O(1) for the pointer management and O(m) for the tensor data allocation, where m is the number of elements in the output tensor (equal to the batch size times the number of channels in this case).</li>
</ul>
</dd></dl>
<div class="fragment"><div class="line">```cpp</div>
<div class="line"><a class="code hl_class" href="classnz_1_1nodes_1_1_node.html">Node</a>* inputNode = <span class="keyword">new</span> <a class="code hl_class" href="classnz_1_1nodes_1_1_node.html">Node</a>(...);</div>
<div class="line"><a class="code hl_class" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">GlobalAvgPoolNode</a> globalAvgPoolNode(inputNode);</div>
<div class="line">```</div>
<div class="ttc" id="aclassnz_1_1nodes_1_1_node_html"><div class="ttname"><a href="classnz_1_1nodes_1_1_node.html">nz::nodes::Node</a></div><div class="ttdoc">Base class for nodes in a neural network or computational graph.</div><div class="ttdef"><b>Definition</b> <a href="_nodes_8cuh_source.html#l00114">Nodes.cuh:114</a></div></div>
</div><!-- fragment --><dl class="section author"><dt>Author</dt><dd>Mgepahmge(<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/07/15 </dd></dl>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00692">692</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>

</div>
</div>
<h2 class="groupheader">Member Function Documentation</h2>
<a id="a7a075619e9c976875118783bbdac8739" name="a7a075619e9c976875118783bbdac8739"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7a075619e9c976875118783bbdac8739">&#9670;&#160;</a></span>backward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void nz::nodes::calc::GlobalAvgPoolNode::backward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Performs the backward pass of the global average pooling operation. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">None</td><td></td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>None</dd></dl>
<p>This function conducts the backward pass of the global average pooling operation. It first checks if the output tensor of the input node requires gradient computation. If it does, the function calls <code>iGlobalAvgPoolBackward</code>, passing the gradient tensor of the input node's output, the gradient tensor of the output, and the shape information of the input tensor (batch size, number of channels, height, and width). The <code>iGlobalAvgPoolBackward</code> function computes the gradients and propagates them back to the input.</p>
<p>Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing gradient tensors of the input and output. Exception handling mechanism: There is no explicit exception handling in this function. If the <code>iGlobalAvgPoolBackward</code> function encounters an error, it may throw an exception, and the specific type of exception depends on the implementation of <code>iGlobalAvgPoolBackward</code>.</p>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">[Exception</td><td>type from iGlobalAvgPoolBackward] If the <code>iGlobalAvgPoolBackward</code> function encounters an error during execution.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>Ensure that the gradient tensors of the input node's output and the output tensor of the <a class="el" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html" title="Performs global average pooling operation across spatial dimensions of input tensor.">GlobalAvgPoolNode</a> are properly initialized before calling this function.</li>
</ul>
</dd></dl>
<div class="fragment"><div class="line">```cpp</div>
<div class="line"><a class="code hl_class" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">GlobalAvgPoolNode</a> globalAvgPoolNode(...); <span class="comment">// Assume GlobalAvgPoolNode is properly initialized</span></div>
<div class="line">globalAvgPoolNode.backward();</div>
<div class="line">```</div>
</div><!-- fragment --><dl class="section author"><dt>Author</dt><dd>Mgepahmge(<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/07/15 </dd></dl>

<p>Implements <a class="el" href="classnz_1_1nodes_1_1_node.html#a0a9ecbaa3d790ba38e8218aca7837fd0">nz::nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00709">709</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>

</div>
</div>
<a id="ad8b2bb2ce47ab3c227f8c33f3a19fb91" name="ad8b2bb2ce47ab3c227f8c33f3a19fb91"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad8b2bb2ce47ab3c227f8c33f3a19fb91">&#9670;&#160;</a></span>forward()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">void nz::nodes::calc::GlobalAvgPoolNode::forward </td>
          <td>(</td>
          <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">override</span><span class="mlabel">virtual</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Performs the forward pass of the global average pooling operation. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">None</td><td></td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>None</dd></dl>
<p>This function conducts the forward pass of the global average pooling operation. It iterates over each sample in the batch (<code>i</code> loop) and each channel (<code>j</code> loop) of the input tensor. For each combination of batch and channel, it calculates the sum of all elements in the corresponding 2D matrix of the input tensor using the <code>sum</code> method. Then, it divides this sum by the total number of elements in the 2D matrix (which is the product of the height and width of the input tensor). The result is then used to fill the corresponding element in the output tensor using the <code>fillMatrix</code> method.</p>
<p>Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing data tensors of the input and output. Exception handling mechanism: There is no explicit exception handling in this function. If the <code>sum</code> or <code>fillMatrix</code> methods encounter an error, they may throw exceptions depending on their implementation.</p>
<dl class="exception"><dt>Exceptions</dt><dd>
  <table class="exception">
    <tr><td class="paramname">[Exception</td><td>type from sum or fillMatrix] If the <code>sum</code> or <code>fillMatrix</code> methods encounter an error during execution.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>Ensure that the input and output tensors are properly initialized before calling this function.</li>
<li>The time complexity of this function is O(b * c * h * w), where b is the batch size, c is the number of channels, h is the height, and w is the width of the input tensor, because it needs to sum all elements in each 2D matrix of the input tensor.</li>
</ul>
</dd></dl>
<div class="fragment"><div class="line">```cpp</div>
<div class="line"><a class="code hl_class" href="classnz_1_1nodes_1_1calc_1_1_global_avg_pool_node.html">GlobalAvgPoolNode</a> globalAvgPoolNode(...); <span class="comment">// Assume GlobalAvgPoolNode is properly initialized</span></div>
<div class="line">globalAvgPoolNode.forward();</div>
<div class="line">```</div>
</div><!-- fragment --><dl class="section author"><dt>Author</dt><dd>Mgepahmge(<a href="https://github.com/Mgepahmge">https://github.com/Mgepahmge</a>)</dd></dl>
<dl class="section date"><dt>Date</dt><dd>2024/07/15 </dd></dl>

<p>Implements <a class="el" href="classnz_1_1nodes_1_1_node.html#a8a828c2e91a4aa2a9ab7b94554e4685b">nz::nodes::Node</a>.</p>

<p class="definition">Definition at line <a class="el" href="_nodes_8cu_source.html#l00700">700</a> of file <a class="el" href="_nodes_8cu_source.html">Nodes.cu</a>.</p>

</div>
</div>
<hr/>The documentation for this class was generated from the following files:<ul>
<li>D:/Users/Mgepahmge/Documents/C Program/NeuZephyr/include/NeuZephyr/<a class="el" href="_nodes_8cuh_source.html">Nodes.cuh</a></li>
<li>D:/Users/Mgepahmge/Documents/C Program/NeuZephyr/src/<a class="el" href="_nodes_8cu_source.html">Nodes.cu</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0
</small></address>
</div><!-- doc-content -->
</body>
</html>
